﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;

namespace WebApplication1
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    //[WebService(Namespace = "http://tempuri.org/")]
    //[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class navHL : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {

            context.Response.ContentType = "text/plain";
            int newsId = 0;
            if (context.Request.QueryString["id"] != null)
                newsId = int.Parse(context.Request.QueryString["Id"]);
            var callback = context.Request.QueryString["callback"];
            string str = "";
            try
            {
                if (newsId > 0)
                {

                    using (var db = new fcmsEntities())
                    {
                        // lay ra 1 object muon lay ,cach lay du~ lieu. tuy`, o day lay bang data entity
                        var news = db.news.Where(en => en.id == newsId).FirstOrDefault();

                        if (news != null)
                        {
                            //convert  object sang JSON
                            str = JsonConvert.SerializeObject(new ResultRespon()
                            {
                                Message = "SUCCESS",
                                Value = news

                            });
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                str = JsonConvert.SerializeObject(new ResultRespon()
                {
                    Message = "ERROR",
                    Value = ex.Message
                });
            }
            WriteResult(context, callback, str);

        }
        private void WriteResult(HttpContext context, object callback, string str)
        {
            if (callback != null)
            {
                context.Response.Write(callback + "(" + str + ");");
            }
            else
            {
                context.Response.Write(str);
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    public class ResultRespon
    {
        public string Message { get; set; }
        public object Value { get; set; }
    }
}
